import Vue from 'vue'
import { Toast, Dialog } from 'vant';
Vue.use('Toast', Toast) //消息
Vue.use('Dialog', Dialog) //弹窗

//文字消息提示
function msg(_msg, _options) {

    var time = (_options ? _options.time || 1000 : 1000) / 1000;
    var end = _options ? _options.end || null : null;

    const toast = Toast({
        duration: 0, // 持续展示 toast
        forbidClick: true, // 禁用背景点击
        message: _msg,
        position: 'bottom'
    });
    const timer = setInterval(() => {
        time--;
        if (!time) {
            typeof end === "function" && end();
            clearInterval(timer);
            Toast.clear();
        }
    }, 1000);
    return toast;
}

function dialog(_msg, _options) {
    Dialog.confirm({
        title: '提示',
        message: _msg,
        showCancelButton: false,
        overlay: true,
        closeOnClickOverlay: false,
        lockScroll: true
    }).then(() => {
        typeof _options.end === "function" && _options.end();
    }).catch(() => {
        typeof _options.end === "function" && _options.end();
    });
}

function error(_msg, _options) {
    var end = _options ? _options.end || null : null;
    Dialog.alert({
        title: '错误',
        message: _msg,
        overlay: true,
        closeOnClickOverlay: true,
        lockScroll: true
    }).then(() => {

        typeof end === "function" && end();
    });
}

function success(_msg, _options) {
    var end = _options ? _options.end || null : null;
    Dialog.alert({
        message: _msg,
    }).then(() => {
        typeof end === "function" && end();
    })
}

function alert(_options) {
    Dialog(_options);
}
//加载
function loading(_msg) {
    Toast.loading({
        mask: true,
        message: (_msg && _msg.length > 0) ? _msg : '加载中...',
        duration: 0,
        forbidClick: true,
    });
}

function close() {
    Toast.clear();
}

function confirm(_options) {
    Dialog.confirm({
        title: _options.title || '提示',
        message: _options.msg,
        confirmButtonText: _options.confirmButtonText || '确定'
    }).then(() => {
        typeof _options.confirm === "function" && _options.confirm();
    }).catch(() => {
        typeof _options.cancel === "function" && _options.cancel();
    });
}
export default {
    msg: msg,
    error: error,
    success: success,
    alert: alert,
    loading: loading,
    close: close, //关闭
    confirm: confirm, //消息提示
    dialog: dialog,
}